<?php
if (!empty($allData)){
        $postion = $postion ==1?'':'none';
        foreach ($allData as $k => $v) {
            //----资产包----start---
            if ($v['_index'] == "package"){
                $package_title         = !empty($v['_source']['package_title'])?$v['_source']['package_title']:"";        //标题
                $package_creditor      = !empty($v['_source']['creditor'])?$v['_source']['creditor']:""; //持包人名称
                $package_code      = !empty($v['_source']['code'])?$v['_source']['code']:""; //资产包编号
                $package_principal      = !empty($v['_source']['principal'])?\common\helps\formatMoney::format_money($v['_source']['principal']):""; //资产包本金
                $package_interest      = !empty($v['_source']['interest'])?\common\helps\formatMoney::format_money($v['_source']['interest']):""; //资产包利息
                $package_hushu      = !empty($v['_source']['hushu'])?$v['_source']['hushu']:""; //资产包剩余户数
                $package_created_at = !empty($v['_source']['created_at'])?$v['_source']['created_at']:"";//上传时间
                $timeCreate = mb_substr($package_created_at,0,mb_stripos($package_created_at,'T'));
                $package_state      = !empty(Yii::$app->params['package_state'][$v['_source']['state']])?Yii::$app->params['package_state'][$v['_source']['state']]:"";//状态

                //--------联系人---start--
                $contact = $phone  = "";
                if (!empty($package_transfer)){
                    foreach ($package_transfer as $k1 => $v1) {
                        if ($v1['_source']['package_id'] == $v["_id"]){
                            $contact = !empty($v1['_source']['contact'])?$v1['_source']['contact']:""; //联系人
                            $phone   = !empty($v1['_source']['phone'])?$v1['_source']['phone']:""; //电话',
                        }
                    }
                }
                //--------联系人----end---
                //-----资产包负责人-----start--
                $leader = $owner = $description = "";
                if (!empty($package_initiation)){
                    foreach ($package_initiation as $k1 => $v1) {
                        if ($v1['_source']['package_id'] == $v["_id"]){
                            $leaderId = !empty($v1['_source']['leader'])?str_replace(',', '', $v1['_source']['leader']):""; //资产包负责人ID
                        }
                    }
                    if (!empty($leaderId)&&!empty($user)){
                        foreach ($user as $k2 => $v2) {
                            if ($v2['_id'] == $leaderId){
                                $leader = !empty($v2['_source']['realname'])?$v2['_source']['realname']:""; //资产包负责人
                                if (!empty($auth_item)){
                                    foreach ($auth_item as $k3 => $v3){
                                        if ($v3['_source']['name'] == $v2['_source']['department']){
                                            $description = !empty($v3['_source']['description'])?$v3['_source']['description']:""; //资产包负责人的部门
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                $leaderId = !empty($v['_source']['owner'])?$v['_source']['owner']:""; //资产包负责人
                if (!empty($leaderId)&&!empty($user)){
                    foreach ($user as $k2 => $v2) {
                        if ($v2['_id'] == $leaderId){
                            $owner = !empty($v2['_source']['realname'])?$v2['_source']['realname']:""; //资产包负责人
                            if (!empty($auth_item)){
                                foreach ($auth_item as $k3 => $v3){
                                    if ($v3['_source']['name'] == $v2['_source']['department']){
                                        $description = !empty($v3['_source']['description'])?$v3['_source']['description']:""; //资产包负责人的部门
                                    }
                                }
                            }
                        }
                    }
                }
                $leader = !empty($leader)?$leader:$owner;
                //-----资产包负责人-----end--
                //----- 尽调成员---start---
                $due_diligence = '';
                if (!empty($package_initiation)){
                    foreach ($package_initiation as $k1 => $v1) {
                        if ($v1['_source']['package_id'] == $v["_id"]){
                            $due_diligence_id = !empty($v1['_source']['due_diligence'])?$v1['_source']['due_diligence']:""; //资产包负责人ID
                        }
                    }
                    if (!empty($due_diligence_id)&&!empty($user)){
                        $due_diligence_arr = explode(',',$due_diligence_id);
                        foreach ($due_diligence_arr as $k3 => $v3) {
                            foreach ($user as $k2 => $v2) {
                                if ($v2['_id'] == $v3){
                                    $due_diligence.= !empty($v2['_source']['realname'])?$v2['_source']['realname'].',':""; //资产包负责人
                                }
                            }
                        }
                    }
                }
                //-----尽调成员-----end--
                //---table-------start---
                //---权限
                $filterPackage = \backend\controllers\DtActionFilter::checkPackage("package_view",$v['_id']);
                $Redpackage_title = preg_replace($normalStr, $tagStr, $package_title);
                $packageId = $v['_id'];
                if ($filterPackage){
                    $packageUrl = "<a href='/package/detail-info?packageid=$packageId' target='_blank'><span class='pull-left'>".$Redpackage_title."</span></a>";
                    $packageFilterHtml = '';
                }else{
                    $packageUrl =  <<<AURL
<a onclick="lmsg($packageId,'$package_title',1)"><span class='pull-left'>$Redpackage_title</span></a>
AURL;

                    $packageFilterHtml = '<div class="pull-right permission">无访问权限</div>';
                }


                if (!empty($package_state)){
                    $statesHtml = '<div class="pull-right project">'.$package_state.'</div>';
                }else{
                    $statesHtml = '';
                }
                echo <<<TABLEPACAKGE
            <div class="searchLists clearfix searchLists-pakage" style="display: $postion">
                 <div class="searchLists-nav clearfix">
                    $packageUrl
                    <div class="pull-left package">资产包</div>
                    $packageFilterHtml
                    $statesHtml
                </div>
                <div class="searchLists-content">
                    <span>持包人名称：<font>$package_creditor</font></span>
                    <span>本金：	<font>$package_principal</font></span>
                    <span>利息： <font>$package_interest</font></span>
                    <span>剩余户数： <font>$package_hushu 户</font></span></br>
                    <span>联系人： <font>$contact</font></span>
                    <span>电话： <font>$phone</font></span>
                    <span>资产包负责人： <font>$description - $leader</font></span>
                    <span>尽调成员： <font>$due_diligence</font></span>
                    
         		</div>
                <div class="sc">
                    <span class="pull-right">  $timeCreate $owner 上传</span>
                </div>
           </div>
TABLEPACAKGE;
                //---table-------end---

            }
            //----资产包----end---
//-------------------------------------------------------------------------

            //----项目----start---
            if ($v['_index'] == "project"){
                $project_pid           = !empty($v['_source']['package_id'])?$v['_source']['package_id']:0;
                $project_title         = !empty($v['_source']['project_title'])?$v['_source']['project_title']:"";        //标题
                $project_borrower      = !empty($v['_source']['borrower'])?$v['_source']['borrower']:""; //持包人名称
                $project_code      = !empty($v['_source']['code'])?$v['_source']['code']:""; //项目编号
                $project_principal      = !empty($v['_source']['principal'])?\common\helps\formatMoney::format_money($v['_source']['principal']):""; //项目本金

                $project_interest      = !empty($v['_source']['interest'])?\common\helps\formatMoney::format_money($v['_source']['interest']):""; //项目利息
                $project_created_at      = !empty($v['_source']['created_at'])?$v['_source']['created_at']:""; //项目利息
                $timeCreate = mb_substr($project_created_at,0,mb_stripos($project_created_at,'T'));
                $project_status      = !empty(Yii::$app->params['projectState'][$v['_source']['status']]['name'])?Yii::$app->params['projectState'][$v['_source']['status']]['name']:"";//状态

                $cityId = !empty($v['_source']['city'])?$v['_source']['city']:""; //城市ID
                $provinceId = !empty($v['_source']['province'])?$v['_source']['province']:""; //省份ID
                $project_city = "";
                $project_province = ''; //省份

                if (!empty($cityId)){
                    $project_city = \backend\models\SearchPackage::getCity($cityId)[0]['title'];     //城市
                }
                if (!empty($provinceId)){
                    $project_province = \backend\models\SearchPackage::getCity($provinceId)[0]['title'];     //城市
                }

                //------项目所在资产包
                $packageInfo = \backend\models\SearchPackage::getPackageOfPackId($project_pid);
                $packageOwner = !empty($packageInfo['owner'])?$packageInfo['owner']:'';
                $packageTitle = !empty($packageInfo['title'])?$packageInfo['title']:'';
                $packageCreditor = !empty($packageInfo['creditor'])?$packageInfo['creditor']:'';

                //-----项目经理-----start--
                $manager  = $owner = $managerId = $description1 =$description2 = "";
                if (!empty($project_team)){
                    foreach ($project_team as $k1 => $v1) {
                        if ($v1['_source']['project_id'] == $v["_id"]){
                            $managerId = !empty($v1['_source']['owner'])?str_replace(',', '', $v1['_source']['owner']):""; //资产包负责人ID
                        }
                    }
                    if (!empty($managerId)&&!empty($user)){
                        foreach ($user as $k2 => $v2) {
                            if ($v2['_id'] == $managerId){
                                $manager = !empty($v2['_source']['realname'])?$v2['_source']['realname']:""; //资产包负责人
                                if (!empty($auth_item)&&!empty($manager)){
                                    foreach ($auth_item as $k3 => $v3){
                                        if ($v3['_source']['name'] == $v2['_source']['department']){
                                            $description1 = !empty($v3['_source']['description'])?$v3['_source']['description']:""; //资产包负责人的部门
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                $leaderId = !empty($packageOwner)?$packageOwner:""; //上传人
                if (!empty($leaderId)&&!empty($user)){
                    foreach ($user as $k2 => $v2) {
                        if ($v2['_id'] == $leaderId){
                            $owner = !empty($v2['_source']['realname'])?$v2['_source']['realname']:""; //资产包负责人
                            if (!empty($auth_item)){
                                foreach ($auth_item as $k3 => $v3){
                                    if ($v3['_source']['name'] == $v2['_source']['department']){
                                        $description2 = !empty($v3['_source']['description'])?$v3['_source']['description']:""; //资产包负责人的部门
                                    }
                                }
                            }
                        }
                    }
                }
//                $manager = !empty($manager)?$manager:$owner;
                //-----项目经理-----end--
                //----- 项目协助人员---start---
                $partner = '';
                if (!empty($project_team)){
                    $partner_id = "";
                    foreach ($project_team as $k1 => $v1) {
                        if ($v1['_source']['project_id'] == $v["_id"]){
                            $partner_id = !empty($v1['_source']['partner'])?$v1['_source']['partner']:""; //资产包负责人ID
                        }
                    }
                    if (!empty($partner_id)&&!empty($user)){
                        $partner_id_arr = explode(',',$partner_id);
                        foreach ($partner_id_arr as $k3 => $v3) {
                            foreach ($user as $k2 => $v2) {
                                if ($v2['_id'] == $v3){
                                    $partner.= !empty($v2['_source']['realname'])?$v2['_source']['realname'].',':""; //资产包负责人
                                }
                            }
                        }
                    }
                }
                //-----项目协助人员-----end--

                //---table-------start---
                //---权限
                $filterProject = \backend\controllers\DtActionFilter::checkProject("project_view",$v['_id']);
                $Redproject_title = preg_replace($normalStr, $tagStr, $project_title);
                $project_id = $v['_id'];
                if ($filterProject){
                    $projectUrl = "<a href='/project/getinfodata?package_id=$project_pid&project_id=$project_id' target='_blank'><span class='pull-left'>".$Redproject_title."</span></a>";
                    $prijectFilterHtml = "";
                }else{
                    $prijectFilterHtml = '<div class="pull-right permission">无访问权限</div>';
                    $projectUrl = <<<PROJECTURL
<a onclick="lmsg($project_id,'$project_title',2)"><span class='pull-left'>$Redproject_title</span></a>
PROJECTURL;
                }


                //--无状态不显示状态标签
                if (!empty($project_status)&&$v['_source']['status']>1){
                    $statesHtml = '<div class="pull-right project">'.$project_status.'</div>';
                }else{
                    $statesHtml = '';
                }
                //--隶属资产包权限
                $filterPackage = \backend\controllers\DtActionFilter::checkPackage("package_view",$project_pid);
                if ($filterPackage){
                    $packageUrl = <<<PACKAGEURLHTML
<a href="/package/detail-info?packageid=$project_pid">$packageTitle</a>
PACKAGEURLHTML;
                }else{
                    $packageUrl = <<<PACKAGEURLHTML
<a onclick="lmsg($project_pid,'$packageTitle',1)">$packageTitle</a>
PACKAGEURLHTML;
                }
                echo <<<TABLEPROJECT
            <div class="searchLists clearfix searchLists-pakage" style="display: $postion">
                 <div class="searchLists-nav clearfix">
                    $projectUrl
                    <div class="pull-left package">项目</div>
                    $prijectFilterHtml
                    $statesHtml
                </div>
                <div class="searchLists-content">
                    <span>持包人名称：<font>$packageCreditor</font></span>
                    <span>本金：	<font>$project_principal</font></span>
                    <span>利息： <font>$project_interest</font></span>
                    <span>省份地区： <font> $project_province - $project_city</font></span></br>
                    <span>隶属资产包： <font>$packageUrl</font></span>
                    <span>项目经理： <font>$description1 - $manager</font></span>
                    <span>团队成员： <font>$partner</font></span>
         		</div>
         		<div class="sc">
                    <span class="pull-right">$timeCreate $owner 上传</span>
                </div>
           </div>
TABLEPROJECT;
                //---table-------end---
            }
            //----项目----end---
        }
}
?>
<script>
    function lmsg(id,title,is_package) {
        var title = title;
        $("#packageId").val(id);
        $("#shenqingFW").modal();
        $("#title").val(title)
        $("#is_package").val(is_package)
    }
    //------------------------------申请访问----fly------------------
    $(function () {
        $("#saveFWAccess").click(function () {
            var shenqingFWNote = $("#shenqingFWNote").val();
            var packageId = $("#packageId").val();
            var title = $("#title").val();
            var is_package = $("#is_package").val();
            var group = 1;
            if (is_package==1){
                group = 1
            }else{
                group = 2
            }
            var url = '/package-apply/access-apply'
            if (shenqingFWNote!=''){
                $.post(url,{'packageId':packageId,'shenqingFWNote':shenqingFWNote,'title':title,'group':group,'is_package':is_package},function (res) {
                    resData = JSON.parse(res)
                    layer.msg(resData.message)
                    if (resData.code == 200){
                        $("#shenqingFW").modal('hide');
                    }
                })
            }else{
                layer.msg('请填写访问申请理由');
            }
        })
    })
    //------------------------------申请访问----fly------------------

</script>


